package com.hyperboost.service.impl;

import com.github.pagehelper.PageHelper;
import com.hyperboost.constant.MessageConstant;
import com.hyperboost.dto.FrontUserDTO;
import com.hyperboost.dto.SharePageQueryDto;
import com.hyperboost.entity.FrontUser;
import com.hyperboost.exception.*;
import com.hyperboost.mapper.UserMapper;
import com.hyperboost.result.PageResult;
import com.hyperboost.service.UserService;
import com.hyperboost.utils.ThreadLocalUtil;
import com.hyperboost.vo.FrontUserVO;
import com.hyperboost.vo.ShareVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     *
     * @param frontUserDto
     */
    @Override
    public void register(FrontUserDTO frontUserDto) {

        FrontUser frontUser = new FrontUser();
        BeanUtils.copyProperties(frontUserDto, frontUser);

        // 1.查询数据库中是否存在该用户名
        FrontUser sysUser = userMapper.getByUsername(frontUser.getUsername());
        if (sysUser != null) {
            // 2。用户名已存在，抛出AccountExistsException异常
            throw new AccountExistsException(MessageConstant.ACCOUNT_EXISTS);
        }
        // 3.用户名不存在，调用userMapper.register()方法
        frontUser.setPassword(DigestUtils.md5DigestAsHex(frontUser.getPassword().getBytes()));
        userMapper.register(frontUser);
    }

    /**
     * 用户登录
     *
     * @param frontUserDto
     * @return FrontUserVo
     */
    @Override
    public FrontUserVO login(FrontUserDTO frontUserDto) {

        // 1.查询数据库中是否存在该用户名
        FrontUser sysUser = userMapper.getByUsername(frontUserDto.getUsername());

        if (sysUser == null) {
            // 2.用户名不存在，抛出AccountNotExistsException异常
            throw new AccountNotExistsException(MessageConstant.ACCOUNT_NOT_EXISTS);
        }
        // 3.用户名存在，验证密码是否正确
        if (!sysUser.getPassword().equals(DigestUtils.md5DigestAsHex(frontUserDto.getPassword().getBytes()))) {
            // 4.密码错误，抛出PasswordErrorException异常
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        // 4.密码正确，校验用户账号状态
        if (sysUser.getStatus() == 0) {
            // 5.账号已被禁用，抛出AccountDisabledException异常
            throw new AccountDisabledException(MessageConstant.ACCOUNT_DISABLED);
        }
        // 6.登录成功，返回FrontUser对象
        FrontUserVO frontUserVo = new FrontUserVO();
        BeanUtils.copyProperties(sysUser, frontUserVo);
        return frontUserVo;
    }

    /**
     * 根据用户Id获取用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public FrontUserVO getUserByUserId(Long userId) {

        // 1.根据用户Id获取用户信息，调用userMapper.getUserByUserId()方法
        FrontUser frontUser = userMapper.getUserByUserId(userId);

        FrontUserVO frontUserVo = new FrontUserVO();
        BeanUtils.copyProperties(frontUser, frontUserVo);

        // 2.返回FrontUserVo对象
        return frontUserVo;
    }

    @Override
    public void update(FrontUserDTO frontUserDto) {

        FrontUser frontUser = new FrontUser();
        BeanUtils.copyProperties(frontUserDto, frontUser);

        // 1.更新用户信息，调用userMapper.update()方法
        userMapper.update(frontUser);
    }

    @Override
    public void password(Map<String, String> passwordMap) {

        // 1.获取用户新旧密码参数，调用passwordMap.get()方法
        String oldPassword = passwordMap.get("oldPassword");
        String newPassword = passwordMap.get("newPassword");
        String confirmPassword = passwordMap.get("confirmPassword");

        // 2.新旧密码参数校验
        if (!StringUtils.hasLength(oldPassword) ||
                !StringUtils.hasLength(newPassword) ||
                !StringUtils.hasLength(confirmPassword)) {
            // 3.密码参数不能缺少，抛出ParameterMissingException异常
            throw new ParameterMissingException(MessageConstant.PASSWORD_PARAM_MISSING);
        }

        // 4.获取当前用户系统旧密码调用userMapper.getUserByUsername()方法
        Map<String, Object> map = ThreadLocalUtil.get();
        Long userId = ((Number) map.get("userId")).longValue();
        FrontUser frontUser = userMapper.getUserByUserId(userId);
        String sysPassword = frontUser.getPassword();

        // 5.校验当前用户系统旧密码与输入旧密码是否一致
        if (!sysPassword.equals(DigestUtils.md5DigestAsHex(oldPassword.getBytes()))) {
            // 5.旧密码错误，抛出PasswordErrorException异常
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 6.校验新密码与确认密码是否一致
        if (!newPassword.equals(confirmPassword)) {
            // 7.新密码与确认密码不一致，抛出PasswordNotMatchException异常
            throw new PasswordNotMatchException(MessageConstant.PASSWORD_NOT_MATCH);
        }
        // 8.更新用户密码，调用userMapper.update()方法
        frontUser.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
        userMapper.password(frontUser);
    }


}
